﻿#pragma once

#include <QTreeWidget>
#include <QContextMenuEvent>
#include <QMenu>
#include <QAction>

#include "vtkSmartPointer.h"
#include "vtkActor.h"
#include "SelectionManager.h"
#include "ActorManager.h"
#include "ActorManager.h"
#include "MyVtkInteractorSelect.h"

// 前向声明

class CustomInteractorStyle;

// TreeWidget中存储Actor指针的自定义角色
const int ActorPointerRole = Qt::UserRole + 1;
const int ActorModeRole = Qt::UserRole + 2;

class ModelTreeWidget: public QTreeWidget
{
    Q_OBJECT

public:
    explicit ModelTreeWidget(QWidget* parent = nullptr);
    ~ModelTreeWidget();

    // 设置管理器
    void setManagers(std::shared_ptr<SelectionManager>SelectionManager,
                     std::shared_ptr<ActorManager> actorManager,
                     vtkSmartPointer<MyVtkInteractorSelect> interactorStyle);

    // 更新树形控件
    void updateTree();

    // 同步选择状态
    void syncSelectionFromVTK();
    void syncSelectionToVTK();

public slots:
    void onDeleteSelected();
    void onClearAll();
    void onMultiPickToggled(bool enabled);
    void onPickModeChanged();

signals:
    void requestRender();  // 通知外部需要渲染
    void objectCountChanged(int count);// 对象数量变化

protected:
    void contextMenuEvent(QContextMenuEvent* event) override;

private slots:
    void onItemClicked(QTreeWidgetItem* item, int column);
    void onItemSelectionChanged();

private:
    void setupUI();
    void setupConnections();
    QTreeWidgetItem* findTreeItemByActor(vtkActor* actor);
    void updateItemAppearance(QTreeWidgetItem* item, bool isHighlighted);

    // 管理器
    std::shared_ptr<SelectionManager> m_highlightManager;
    std::shared_ptr<ActorManager> m_actorManager;
    vtkSmartPointer<MyVtkInteractorSelect> m_interactorStyle;

    // UI组件
    QMenu* m_contextMenu;
    QAction* m_actionCreateSphere;
    QAction* m_actionCreateCube;
    QAction* m_actionCreateCylinder;
    QAction* m_actionDeleteSelected;
    QAction* m_actionClearAll;

    // 防止循环同步的标志
    bool m_blockSelectionSync;
};
